package demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

@SpringBootApplication
public class DemoApplication {

 public static void main(String[] args) {
  SpringApplication.run(DemoApplication.class, args);
 }
}

@RestController
class SessionController {

 private final String ip;

 @Autowired
 public SessionController(@Value("${CF_INSTANCE_IP:127.0.0.1}") String ip) {
  this.ip = ip;
 }

 @GetMapping("/hi")
 Map<String, String> uid(HttpSession session) {
  // <1>
  UUID uid = Optional.ofNullable(UUID.class.cast(session.getAttribute("uid")))
   .orElse(UUID.randomUUID());
  session.setAttribute("uid", uid);

  Map<String, String> m = new HashMap<>();
  m.put("instance_ip", this.ip);
  m.put("uuid", uid.toString());
  return m;
 }

}